package com.systemdevelopment.mainproject.Model;

import java.sql.*;
import java.util.HashSet;

public class NormalUser extends User {
    static final String Authority = "NORMALUSER";
    protected HashSet<NormalUser> NormalUserSet;
    public String getAuthority()
    {
        return Authority;
    }
    public NormalUser() {
    }

    public NormalUser(String Account, String Password) {
        this.Account = Account;
        this.Password = Password;
    }

    public static void main(String[] args) {        //测试代码

        NormalUser normalUser = GetNormalUserFromDBByAccount("1001");  //测试1  输入账号查询
        System.out.println("该用户密码为" + normalUser.getPassword());


        /*HashSet<NormalUser> normalUserHashSet = new HashSet<>();      //测试2  插入多个NormalUser到READER表
        NormalUser normalUser1 = new NormalUser("1001","password1001",);
		NormalUser normalUser2 = new NormalUser("1002","password1002",);
        normalUserHashSet.add(normalUser1);
        normalUserHashSet.add(normalUser2);
		InsertNormalUserIntoDB(normalUserHashSet);*/


    }

    //更改用户信息（更改密码）
    public static void UpdateNormalUserByAccount(String Account, String new_password) throws SQLException {
        Connection conn = DataBaseManager.build_conn();
        PreparedStatement preSql;

        String sqlStr = "update LIB.READER set PASSWORD = ? where ACCOUNT = ?";

        try {
            preSql = conn.prepareStatement(sqlStr);
            preSql.setString(1, new_password);
            preSql.setString(2, Account);

            preSql.executeUpdate();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //从数据库READER account_manager表中删除一行，参数是ACCOUNT
    public static void DeleteNormalUserFromDB(String Account) throws SQLException {
        try (Connection conn = DataBaseManager.build_conn()) {
            String sql = "DELETE FROM LIB.READER WHERE ACCOUNT = '" + Account + "'";
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(sql);

            //删除account_manager中账户
            Admin.DeleteAccountFromDB(Account);

        } catch (SQLException Se) {
            Se.printStackTrace();
        }
    }

    //从数据库READER表中删除一行，参数是ACCOUNT
    public static void DeleteNormalUserFromREADER(String Account) throws SQLException {
        try (Connection conn = DataBaseManager.build_conn()) {
            String sql = "DELETE FROM LIB.READER WHERE ACCOUNT = '" + Account + "'";
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(sql);

        } catch (SQLException Se) {
            Se.printStackTrace();
        }
    }


    //查询 根据姓名从数据库的READER表中检索用户
    public static NormalUser GetNormalUserFromDBByAccount(String Account) {
        NormalUser normalUser = new NormalUser();
        try {
            Connection conn = DataBaseManager.build_conn();

            String Sql = "select * from LIB.READER where ACCOUNT = " + "'" + Account + "'";

            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(Sql);
            rs.next();
            String account = rs.getString("ACCOUNT");
            String password = rs.getString("PASSWORD");

            if (account.length() > 0 && password.length() > 0) {
                normalUser.setAccount(account);
                normalUser.setPassword(password);
            } else {
                //没找到此账户，异常处理
            }
        } catch (SQLException Se) {
            Se.printStackTrace();
        }
        return normalUser;
    }


    //向数据库的READER表中插入多行，待插入的normaluser存储在HashSetMap<NormalUser>中
    public static boolean InsertNormalUserIntoDB(NormalUser... NormalUser) {
        try {
            Connection conn = DataBaseManager.build_conn();
            Statement stmt = conn.createStatement();

            for (NormalUser nu : NormalUser) {
                String account = nu.getAccount();
                String password = nu.getPassword();

                String Sql = "Insert into LIB.READER VALUES(" + "'" + account + "'" + "," + "'" + password + "'" + ")";

                stmt.addBatch(Sql);
            }
            int[] num = stmt.executeBatch();

            return true;

        } catch (SQLException Se) {
            Se.printStackTrace();
        }
        return false;
    }

    public static void InsertNormalUserIntoDB(HashSet<NormalUser> NormalUserSet) {
        for (NormalUser nu :
                NormalUserSet) {
            InsertNormalUserIntoDB(nu);
        }
    }


    //获取所有NormalUser
    public static HashSet<NormalUser> GetAllNormalUserFromDB() {
        HashSet<NormalUser> NormalUserSet = new HashSet<>();

        try {
            Connection conn = DataBaseManager.build_conn();

            String Sql = "select * from LIB.READER";

            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(Sql);

            while (rs.next()) {
                String account = rs.getString("ACCOUNT");
                String password = rs.getString("PASSWORD");

                if (account.length() > 0 && password.length() > 0) {
                    NormalUser normalUser = new NormalUser();
                    normalUser.setAccount(account);
                    normalUser.setPassword(password);
                    NormalUserSet.add(normalUser);
                }
            }
        } catch (SQLException Se) {
            Se.printStackTrace();
        }

        return NormalUserSet;
    }
}